package com.czy.owner.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.EmployessModel;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.glide.GlideUtils;
import com.czy.owner.widget.RoundImageView;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;


/**
 * Created by wangyang on 2016/9/22.
 */

public class EmployeesAdapter extends RecyclerArrayAdapter<EmployessModel> {

    private Context context;
    private SelectListener selectListener;
    private String employeesIds;
    private String employeesNames;
    public EmployeesAdapter(Context context, String employeesIds, String employeesNames) {
        super(context);
        this.context = context;
        this.employeesIds = employeesIds;
        this.employeesNames = employeesNames;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new EmployeesAdapter.EmployeesViewHolder(parent, context);
    }


    public class EmployeesViewHolder extends BaseViewHolder<EmployessModel> {
        private RoundImageView roundImgLogo;
        private TextView tvTechnivianName;
        private TextView tvCurrentTask;
        private CheckBox cb;

        public EmployeesViewHolder(ViewGroup parent, Context context) {
            super(parent, R.layout.item_service_technician);
            roundImgLogo = $(R.id.img_head_service_technician);
            tvTechnivianName = $(R.id.tv_technivian_name);
            tvCurrentTask = $(R.id.tv_current_task);
            cb = $(R.id.cb);
        }

        @Override
        public void setData(final EmployessModel data) {
            super.setData(data);
            GlideUtils.loadImage(context, data.getUserLogo(), roundImgLogo, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            },R.mipmap.default_icon,R.mipmap.default_icon);

            tvTechnivianName.setText(data.getEmployeesName());
            tvCurrentTask.setText(data.getHasCompletedCount() + "/" + data.getOrderCount());

            cb.setTag(data);
            cb.setChecked(employeesIds.contains(data.getStoreEmployeesId() + ""));
            cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (((EmployessModel)buttonView.getTag()).equals(data)) {
                        if (isChecked) {
                            if (!employeesIds.contains(data.getStoreEmployeesId() + "")) {
                                employeesNames = employeesNames + "," + data.getEmployeesName();
                                employeesIds = employeesIds + "," + data.getStoreEmployeesId();
                                selectListener.onSelectChange(employeesNames, employeesIds);
                            }
                        } else {
                            if (employeesIds.contains(data.getStoreEmployeesId() + "")) {
                                StringBuilder sbIds = new StringBuilder();
                                StringBuilder sbNames = new StringBuilder();
                                int id = data.getStoreEmployeesId();
                                String[] splitIds = employeesIds.split(",");
                                String[] splitNames = employeesNames.split(",");
                                for (int i = 0; i < splitIds.length; i++) {
                                    if (!TextUtils.isEmpty(splitIds[i])) {
                                        if (splitIds[i].equals(id + "")) {
                                        }
                                        else if (TextUtils.isEmpty(splitIds[i])){
                                        }
                                        else {
                                            sbIds.append(splitIds[i] + ",");
                                            if (splitNames.length>i)
                                                sbNames.append(splitNames[i] + ",");
                                        }
                                    }
                                }
                                if (sbNames.toString().equals("")||sbIds.toString().equals("")){
                                    employeesNames="";
                                    employeesIds="";
                                    selectListener.onSelectChange(employeesNames,employeesIds);
                                }
                                else {
                                    employeesNames=sbNames.toString();
                                    employeesIds=sbIds.toString();
                                    selectListener.onSelectChange(employeesNames,employeesIds);
                                }
                            }
                        }
                    }
                }
            });

        }
    }
    public interface SelectListener {
        void onSelectChange(String names, String ids);
    }

    public void setSelectListener(SelectListener selectListener) {
        this.selectListener = selectListener;
    }
}
